import Vue from 'vue'
import VueRouter from 'vue-router'
import http from '@/utils/http'
import Home from '@/views/index/index'
import main from  '@/views/Home'
Vue.use(VueRouter)

const routes = [
  // 路由输入错误，或没有路由，显示首页
  // 登录路由
  {
    path: '/',
    redirect: '/Home',
  },
  {
    path: '/Login',
    name: 'Login',
    component: () => import( /* webpackChunkName: "Login" */ '@/views/Login'),
    meta: { show: true }
  },
  // 注册路由
  {
    path: '/Regist',
    name: 'Regist',
    component: () => import( /* webpackChunkName: "Regist" */ '@/views/Regist'),
    meta: { show: true }
  },
   // 首页路由
   {
    path: '/Home',
    component: Home,
    children: [
      {
        // 总览
        path: '',
        name: 'Home',
        component: main,
      },
      {
        // 容器路由
        path: '/Container',
        name: 'Container',
        component: () => import( /* webpackChunkName: "container" */ '@/views/Container')
      },
      {
        // 用户信息
        path: '/MyInfo',
        name: 'MyInfo',
        component: () => import( /* webpackChunkName: "MyInfo" */ '@/views/MyInfo')
      },
      // 添加容器
      {
        path: '/Container/AddContainer',
        name: 'AddContainer',
        component: () => import( /* webpackChunkName: "AddContainer" */ '@/views/Container/AddContainer')
      },
      // 编辑容器
      {
        path: '/Container/EditContainer/:id',
        name: 'EditContainer',
        component: () => import( /* webpackChunkName: "EditContainer" */ '@/views/Container/EditContainer')
      },
      // 容器日志
      {
        path: '/Container/Logs/:id',
        name: 'Logs',
        component: () => import( /* webpackChunkName: "Logs" */ '@/views/Container/Logs')
      },
      // 检查容器
      {
        path: '/Container/CheckContainer/:id',
        name: 'CheckContainer',
        component: () => import( /* webpackChunkName: "CheckContainer" */ '@/views/Container/CheckContainer')
      },
      // 容器统计信息
      {
        path: '/Container/CountContainer/:id',
        name: 'CountContainer',
        component: () => import( /* webpackChunkName: "CountContainer" */ '@/views/Container/CountContainer')
      },
      // cmd连接容器，控制台
      {
        path: '/Container/ConsoleContainer/:id',
        name: 'ConsoleContainer',
        component: () => import( /* webpackChunkName: "ConsoleContainer" */ '@/views/Container/ConsoleContainer')
      },
      // 容器详情
      {
        path: '/Container/ConContent/:id',
        name: 'ConContent',
        component: () => import( /* webpackChunkName: "ConContent" */ '@/views/Container/ConContent')
      },
      // 镜像路由
      {
        path: '/Image',
        name: 'Image',
        component: () => import( /* webpackChunkName: "Image" */ '@/views/Image')
      },
      // 创建镜像
      {
        path: '/Image/FoundImage',
        name: 'FoundImage',
        component: () => import( /* webpackChunkName: "FoundImage" */ '@/views/Image/FoundImage')
      },
      // 镜像详情
      {
        path: '/Image/ImageContent/:id',
        name: 'ImageContent',
        component: () => import( /* webpackChunkName: "ImageContent" */ '@/views/Image/ImageContent')
      },
      // 数据卷路由
      {
        path: '/DataVolume',
        name: 'DataVolume',
        component: () => import( /* webpackChunkName: "DataVolume" */ '@/views/DataVolume')
      },
      // 添加数据卷
      {
        path: '/DataVolume/AddVolume',
        name: 'AddVolume',
        component: () => import( /* webpackChunkName: "AddVolume" */ '@/views/Volume/AddVolume')
      },
      // 数据卷详情
      {
        path: '/DataVolume/VolumeContent/:id',
        name: 'VolumeContent',
        component: () => import( /* webpackChunkName: "VolumeContent" */ '@/views/Volume/VolumeContent')
      },
      // 网络路由
      {
        path: '/Network',
        name: 'Network',
        component: () => import( /* webpackChunkName: "Network" */ '@/views/Network')
      },
      // 添加网络
      {
        path: '/Network/AddNet',
        name: 'AddNet',
        component: () => import( /* webpackChunkName: "AddNet" */ '@/views/network/AddNet')
      },
      // 网络详情
      {
        path: '/Network/NetContent/:id',
        name: 'NetContent',
        component: () => import( /* webpackChunkName: "NetContent" */ '@/views/network/NetContent')
      },
      // 登记处，登录镜像站
      {
        path: '/Registries',
        name: 'Registries',
        component: () => import( /* webpackChunkName: "Registries" */ '@/views/Registries')
      },
      // 集群
      {
        path: '/Colony',
        name: 'Colony',
        component: () => import( /* webpackChunkName: "Colony" */ '@/views/Colony')
      },
      // 集群
      {
        path: '/Mirrors',
        name: 'Mirrors',
        component: () => import( /* webpackChunkName: "Mirrors" */ '@/views/Mirrors')
      },
      // 应用市场
      {
        path: '/Store',
        name: 'Store',
        component: () => import( /* webpackChunkName: "Store" */ '@/views/Store/index')
      },
      // 部署Install
      {
        path: '/Install',
        name: 'install',
        component: () => import( /* webpackChunkName: "Install" */ '@/views/Store/install')
      }
    ]
  },
  {
    path: '*',
    name: 'All',
    redirect: '/Home',
    component: Home
  },
  
  
]

const router = new VueRouter({
  mode: 'hash',
  // mode: 'history', // 此模式下在部署项目时，需要在用户刷新时让后端返回首页。
  base: process.env.BASE_URL,
  routes,
  scrollBehavior (to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { x: 0, y: 0 }
    }
  }
})

// 全局路由守卫，检测用户是否登录
router.beforeEach((to, from, next) => {
  const user = localStorage.getItem('user')
  if (to.name === 'Regist' || to.name === 'Login') {
    next()
  } else {
    if (!user) {
      //return next({ path: '/Login',replace: true  })
      // 请求后端判断用户时候首次登录
      http({
        url: '/checkreg',
        method: 'GET',
        timeout: 2000000
      }).then(res => {
        if (res.Data) {
          // 不是跳到登录
          next({ path: '/Login' ,replace: true })
        } else {
          // 是跳到注册
          next({ path: '/Regist',replace: true  })
        }
      }).catch((err) => {
        return err
      })
    } else {
      next()
    }
  }
})

export default router
